<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\facade\Db;
//数据库管理
use phpspirit\dbskeleton\Factory;
use phpspirit\dbskeleton\mysql\TableModel;
use phpspirit\dbskeleton\mysql\ColumnModel;

/**
 * @mixin \think\Model
 */
class Dbd extends Model
{
    //获取单条数据表信息以及主键名
    // public static function Dbdfield($name)
    // {
    //     $table = Db::query("show table STATUS like '" . $name . "'");
    //     $majorkey = Db::query("select COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME <> 'dtproperties' AND table_name = '" . $name . "'");
    //     $table[0]['Name'] = str_replace(Db::getConfig('connections.mysql.prefix'), '', $table[0]['Name']);
    //     $table[0]['COLUMN_NAME'] = $majorkey[0]['COLUMN_NAME'];
    //     return $table[0];
    // }
    //获取数据列表
    public static function Dbdlist()
    {
        $tables = Db::query("show table STATUS");
        if ($tables) {
            $data = array();
            foreach ($tables as $k => $v) {
                // dump($v);
                $v['Size'] = format_bytes($v['Data_length'] + $v['Index_length']);
                $data[] = $v;
            }
        } else {
            $data = '';
        }

        return $data;
    }
    //数据表添加
    public static function Dbdadd($param)
    {
        $tablemodel = new TableModel();
        $fieldmodel = new ColumnModel();
        $istable = Db::query('SHOW TABLES LIKE "' . Db::getConfig('connections.mysql.prefix') . $param['table'] . '"'); //检查数据表
        if ($istable) {
            return getBack(1, ['name' => 'table', 'msg' => '当前数据表已存在！']);
        }
        $tablemodel->setCharset('utf8')  //设置编码
            ->setEngine('InnoDB') //设置引擎
            ->setTablename(Db::getConfig('connections.mysql.prefix') . $param['table']) //设置表名
            ->setComment($param['comment']); //表备注
        $fieldmodel->setType('int') //字段类型
            ->setLen('11') //长度
            ->setName($param['id']) //字段名
            ->setIsPk(true) //是否主键
            ->setIncrement(true) //是否自增长
            ->setComment('主键'); //字段备注
        $dbskeleton = Factory::instance(Db::getConfig('connections.mysql.type'), Db::getConfig('connections.mysql.hostname') . ":" . Db::getConfig('connections.mysql.hostport'), Db::getConfig('connections.mysql.database'), Db::getConfig('connections.mysql.username'), Db::getConfig('connections.mysql.password'));
        $res = $dbskeleton->createTable($tablemodel, [$fieldmodel]);
        return $res;
    }
    //数据表删除
    public static function Dbddel($name)
    {
        $tablemodel = new TableModel();
        $tables =  $tablemodel->setTablename($name);
        $dbskeleton = Factory::instance(Db::getConfig('connections.mysql.type'), Db::getConfig('connections.mysql.hostname') . ":" . Db::getConfig('connections.mysql.hostport'), Db::getConfig('connections.mysql.database'), Db::getConfig('connections.mysql.username'), Db::getConfig('connections.mysql.password'));
        $res = $dbskeleton->dropTable($tables);
        return $res;
    }
    //获取数据表字段
    public static function Dbdfields($name)
    {
        $fields = Db::query("show full fields from " . $name); //查询表所有字段信息
        return $fields;
    }

    //获取数据表单条字段
    public static function Dbdfindone($table, $field)
    {
        $res = Db::query("show full fields from " . $table . " where FIELD='" . $field . "'");
        return $res[0];
    }

    //添加数据表字段
    public static function fieldadd($data)
    {
        $tablemodel = new TableModel();
        $fieldmodel = new ColumnModel();
        $dbskeleton = Factory::instance(Db::getConfig('connections.mysql.type'), Db::getConfig('connections.mysql.hostname') . ":" . Db::getConfig('connections.mysql.hostport'), Db::getConfig('connections.mysql.database'), Db::getConfig('connections.mysql.username'), Db::getConfig('connections.mysql.password'));
        $table = $tablemodel->setTablename($data['table']); //表名
        $field = $fieldmodel->setType($data['type']) //字段类型
            ->setLen($data['length']) //长度
            ->setName($data['name']) //字段名
            ->setIsnull($data['null'] == "1" ? true : false) //是否为空
            ->setIsPk($data['key'] == "1" ? true : false) //是否主键
            ->setIncrement($data['increase'] == '1' ? true : false) //是否自增长
            ->setDefaultval($data['default']) //默认为
            ->setComment($data['comment']); //字段备注

        $res = $dbskeleton->addColumn($table, $field); //添加字段
        return $res;
    }

    //删除数据表字段
    public static function fielddel($data)
    {
        $tablemodel = new TableModel();
        $fieldmodel = new ColumnModel();
        $table = $tablemodel->setTablename($data['table']); //表名
        $field = $fieldmodel->setName($data['name']);
        $dbskeleton = Factory::instance(Db::getConfig('connections.mysql.type'), Db::getConfig('connections.mysql.hostname') . ":" . Db::getConfig('connections.mysql.hostport'), Db::getConfig('connections.mysql.database'), Db::getConfig('connections.mysql.username'), Db::getConfig('connections.mysql.password'));
        $res = $dbskeleton->dropColumn($table, $field);
        return $res;
    }
}
